﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using Newtonsoft.Json;
using System.Web.Services;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.CA
{
    public partial class CASubmitOrder : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            hdnUrl.Value = ConfigurationManager.AppSettings["PayWebSite"];
            if (!IsPostBack)
            {
                hidGuid.Value = Request.QueryString["guid"];//订单guid
                hidGroup.Value = Request.QueryString["group"];
                if (hidGroup.Value == "CA03")
                {
                    RequiredFieldValidator7.Visible = true;
                }
                ModelService.CaPayInfo caPayInfo = JsonConvert.DeserializeObject<ModelService.CaPayInfo>(client.GetOrderInfo(hidGuid.Value));//获取订单信息
                if (caPayInfo != null)
                {
                    lbCode.Text = caPayInfo.Code;
                    txtTitle.Text = caPayInfo.iTitle;
                    txtLinkMan.Text = caPayInfo.LinkPerson;
                    txtTel.Text = caPayInfo.officeTel;
                    txtMobile.Text = caPayInfo.mobileTel;
                    txtPostCode.Text = caPayInfo.postcode;
                    txtAddress.Text = caPayInfo.SendAddress;
                    txtNotice.Text = caPayInfo.Notice;
                    lbSum.Text = caPayInfo.SumPay.ToString();
                    string key = System.Configuration.ConfigurationManager.AppSettings["PayWebSitePublicKey"].ToString();
                    string iv = System.Configuration.ConfigurationManager.AppSettings["PayWebSitePrivateKey"].ToString();
                    OrderId.Value = DESHelper.Encrypt(caPayInfo.ID.ToString(), key, iv, System.Text.Encoding.UTF8);
                    ProductType.Value = DESHelper.Encrypt("C", key, iv, System.Text.Encoding.UTF8);
                    Uid.Value = DESHelper.Encrypt(caPayInfo.CorpUserGuid.ToString(), key, iv, System.Text.Encoding.UTF8);
                }
            }
        }

        protected void lbtnUp_Click(object sender, EventArgs e)
        {
            ModelService.CaPayInfo caPayInfo = JsonConvert.DeserializeObject<ModelService.CaPayInfo>(client.GetOrderInfo(hidGuid.Value));
            if (caPayInfo.bCAStatus == 3)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('支付成功！','',CloseWin);", true);
                //RadWindowManager1.RadAlert("支付成功！", 200, 100, "提示", "CloseWin", "");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('支付失败！','',CloseWin);", true);
                //RadWindowManager1.RadAlert("支付失败！", 200, 100, "提示", "CloseWin", "");
            }
        }

        [WebMethod]
        public static string OptOrders(object datas, object status)
        {
            string[] data = datas.ToString().Split('@');
            OrderServiceClient orderclient = new OrderServiceClient();
            string ret = null;
            ModelService.CaPayInfo caPayInfo = new ModelService.CaPayInfo();
            caPayInfo.KeyGUID = Guid.Parse(data[0]);
            caPayInfo.iTitle = data[1];
            caPayInfo.LinkPerson = data[2];
            caPayInfo.SendAddress = data[3];
            caPayInfo.mobileTel = data[4];
            caPayInfo.officeTel = data[5];
            caPayInfo.postcode = data[6];
            caPayInfo.bCAStatus = byte.Parse(status.ToString());
            caPayInfo.Notice = data[7];
            //caPayInfo.TransDate = 
            caPayInfo.OptDate = DateTime.Now;
            string painfo = JsonConvert.SerializeObject(caPayInfo);
            ModelService.OperationResult result = JsonConvert.DeserializeObject<ModelService.OperationResult>(orderclient.SubmitOrder(painfo));
            ret = JsonConvert.SerializeObject(new { Message = result.Message });
            return ret;
        }
    }
}